<script>
	import {
		mapMutations
	} from 'vuex'
	import {
		version
	} from './package.json'
	import {getAppVersion} from "@/api/home.js"
	export default {
		data:{
			 version:"1.0.0"
		},
		onLaunch: function() {
			const token = uni.getStorageSync('token');
			this.$store.dispatch('setToken', token);
			
			// #ifdef APP 
			this.checkVersion();
			// #endif
			// #ifdef H5
			this.version = "1.0.0";
			// #endif
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		globalData: {
			test: ''
		},
		data:{
			version:""
		},
		methods: {
			checkVersion() {
				var that = this;
				plus.runtime.getProperty(plus.runtime.appid, function(inf) {
					that.version = inf.version;
					console.log("当前应用版本：" + that.version);
					that.checkUpdate();
				});
			},
			checkUpdate() {
				var that = this;
				getAppVersion({}).then(res=>{
					if(res.code == 200){
						var arr = res.rows;
						if(arr.length > 0){
							var dict = arr[0];
							var newVer = dict.appVersion;
							console.log(newVer);
							console.log(that.version);
							// debugger
							if (that.version && newVer && (that.version != newVer) && (that.version < newVer)) {
								that.downWgt(dict.appPath); // 下载升级包
							}
							//  else {
							// 	plus.nativeUI.alert("无新版本可更新！");
							// }	
						}
					}
				})
			},
			downWgt(newapk) {
				console.log(newapk);
				var that = this;
				plus.nativeUI.showWaiting("下载更新文件...");
				plus.downloader.createDownload(newapk, {}, function(d, status) {
					if (status == 200) {
						console.log("下载更新文件成功：" + d.filename);
						that.installWgt(d.filename); // 安装wgt包
					} else {
						console.log("下载更新文件失败！");
						plus.nativeUI.alert("下载更新文件失败！");
					}
					plus.nativeUI.closeWaiting();
				}).start();
			},
			installWgt(path) {
				plus.nativeUI.showWaiting("安装更新文件文件...");
				plus.runtime.install(path, {
					force: true
				}, function() {
					plus.nativeUI.closeWaiting();
					console.log("安装更新文件文件成功！");
					plus.nativeUI.alert("应用资源更新完成！", function() {
						plus.runtime.restart();
					});
				}, function(e) {
					plus.nativeUI.closeWaiting();
					console.log("安装更新文件文件失败[" + e.code + "]：" + e.message);
					plus.nativeUI.alert("安装更新文件文件失败[" + e.code + "]：" + e.message);
				});
			},
			...mapMutations(['setUniverifyErrorMsg', 'setUniverifyLogin'])
		}
	}
</script>

<style lang="scss">
	@import '@/uni_modules/uni-scss/index.scss';
	@import './common/uni.css';
	@import '@/static/customicons.css';
	@media screen and (min-width: 768px) {
		body {
			overflow-y: scroll;
		}
	}

	/* 顶栏通栏样式 */
	/* .uni-top-window {
	    left: 0;
	    right: 0;
	} */

	uni-page-body {
		background-color: #f3f3f3 !important;
		min-height: 100% !important;
		height: auto !important;
	}

	.uni-top-window uni-tabbar .uni-tabbar {
		background-color: #fff !important;
	}

	.uni-app--showleftwindow .hideOnPc {
		display: none !important;
	}


</style>
